package com.qidian.tcba.utils.result;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.qidian.tcba.utils.Message;


import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> {
    private Boolean status;
    private String msg;
    private int code; //成功20000 失败20001
    private Map<String, T> data = new HashMap<>();

    public static JsonResult success() {
        JsonResult jsonResult = new JsonResult();
        jsonResult
                .code(ResultCode.SUCCESS.getCode())
                .status(true);
        return jsonResult;
    }

    public static JsonResult error() {
        JsonResult jsonResult = new JsonResult();
        jsonResult
                .code(ResultCode.ERROR.getCode())
                .status(false);
        return jsonResult;
    }

    public JsonResult code(int code) {
        this.setCode(code);
        return this;
    }

    public JsonResult status(Boolean status) {
        this.setStatus(status);
        return this;
    }

    public JsonResult msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public JsonResult msg(Message msg) {
        this.setMsg(msg.toString());
        return this;
    }

    public JsonResult data(String key, T value) {
        this.data.put(key, value);
        return this;
    }

    public JsonResult data(Map<String, T> data) {
        this.setData(data);
        return this;
    }


    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Map<String, T> getData() {
        return data;
    }

    public void setData(Map<String, T> data) {
        this.data = data;
    }
}
